﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;
using Elentok.Common.Wpf.ViewModels.Dialogs;
using System.Windows;
using System.Threading;
using Elentok.Common.Wpf.Dialogs;
using Elentok.Common.Async;
using Elentok.Common.Wpf.Services;
using System.Windows.Media.Imaging;

namespace Elentok.Common.Wpf.Demos.ViewModels
{
  public class DialogsDemoViewModel : DemoViewModel
  {
    public ICommand ButtonsDialogCommand { get; private set; }

    public ICommand ProgressDialogCommand { get; private set; }

    public ICommand ProgressDialog2Command { get; private set; }

    public ICommand ExceptionDialogCommand { get; private set; }

    public ICommand AboutDialogCommand { get; private set; }

    public ICommand WizardDialogCommand { get; private set; }

    private CommonDialogsService dialogsService;

    public DialogsDemoViewModel() : base("Dialogs")
    {
      this.dialogsService = new CommonDialogsService();

      this.WizardDialogCommand = new DelegateCommand<object>(ExecuteWizardDialogCommand);

      this.ButtonsDialogCommand = new DelegateCommand<object>(delegate 
        {
          var viewModel = new ButtonsDialogViewModel();
          viewModel.Title = "Demo Buttons Dialog";
          viewModel.Message = "This is a test buttons dialog\r\nIt only contains the following 4 buttons";
          viewModel.Buttons.Add(new ButtonViewModel { Header = "Button 1" });
          viewModel.Buttons.Add(new ButtonViewModel { Header = "Button 2", DialogResult = false });
          viewModel.Buttons.Add(new ButtonViewModel { Header = "Button 3", IsEnabled = false });
          viewModel.Buttons.Add(new ButtonViewModel { Header = "Button 4"});

          var dialog = new Dialog(viewModel);
          var result = dialog.ShowDialog();

          MessageBox.Show(string.Format("Dialog returned {0}, {1}", result, viewModel.SelectedButton));
        });

      this.ProgressDialogCommand = new DelegateCommand<object>(delegate
        {
          var task = new DelegateAsyncTask("This dialog will close in 3 seconds",
            delegate(IAsyncProgressNotifier notifier)
            {
              for (int i = 0; i < 30; i++)
              {
                Thread.Sleep(100);
                if (notifier.IsPendingCancellation)
                  notifier.NotifyProgress(100, "Cancelled", "Cancelled :-(");
              }

              notifier.NotifyProgress(100, "It's working", "It's working :-)");
            });

          var dialog = new ProgressDialog();
          dialog.Owner = Application.Current.MainWindow;
          dialog.Start(task, true);
          MessageBox.Show(task.Data.ToString());
        });

      this.ProgressDialog2Command = new DelegateCommand<object>(delegate
      {
        var task = new DelegateAsyncTask("This dialog will close in 3 seconds",
          delegate(IAsyncProgressNotifier notifier)
          {
            notifier.NotifyProgress(0, "Hmm...", null);

            for (int i = 0; i < 30; i++)
            {
              Thread.Sleep(100);
              double progress = i / 30.0 * 100.0;

              notifier.NotifyProgress((int)progress, "Working...", null);

              if (notifier.IsPendingCancellation)
              {
                notifier.NotifyProgress((int)progress, "Cancelled", "Cancelled :-(");
                return;
              }
            }
            notifier.NotifyProgress(100, "It's working", "It's working :-)");
          });

        var dialog = new ProgressDialog();
        dialog.Owner = Application.Current.MainWindow;
        dialog.Start(task, true);
        MessageBox.Show(task.Data.ToString());
      });

      this.ExceptionDialogCommand = new DelegateCommand<object>(
        delegate
        {
          var ex = new Exception("My Exception",
            new Exception("My inner exception",
              new Exception("My inner-inner exception")));

          var vm = new ExceptionDialogViewModel(ex);
          var dialog = new Dialog(vm);
          dialog.Owner = Application.Current.MainWindow;
          dialog.ShowDialog();
        });

      this.AboutDialogCommand = new DelegateCommand<object>(
        delegate
        {
          var viewModel = new AboutDialogViewModel()
          {
            HomePageUrl = "http://elentok.blogspot.com",
            Email = "3david@gmail.com",
            License = Licenses.GPL
          };

          #region Credits

          viewModel.Credits.Add(new AboutDialogCredit()
          {
            Header = "FamFamFam",
            Url = "http://www.famfamfam",
            Description = "Icons by Mark James."
          });


          viewModel.Credits.Add(new AboutDialogCredit
          {
            Header = "LumiSoft",
            Url = "http://www.lumisoft.ee/lswww/download/downloads/Net/",
            Description = "The library used to parse the source of the email messages was written by Ivar Lumi." +
            "Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum."
          });

          viewModel.Credits.Add(new AboutDialogCredit
          {
            Header = "DotNetZip",
            Url = "http://dotnetzip.codeplex.com",
            Description = "The library used to read/write zip files."
          });

          viewModel.Credits.Add(new AboutDialogCredit
          {
            Header = "Prism",
            Url = "http://compositewpf.codeplex.com",
            Description = "Composite Application Guidance for WPF."
          });

          viewModel.Credits.Add(new AboutDialogCredit
          {
            Header = "Unity",
            Url = "http://unity.codeplex.com",
            Description = "Dependency Injection."
          });

          #endregion


          viewModel.Show();
        });
    }

    private void ExecuteWizardDialogCommand(object param)
    {
      var vm = new WizardDialogViewModel();

      vm.AddPage(new ImageWizardPageViewModel { Title = "This is page #1",
        Message = "This is the first page,\r\nhmm....",
        Image = new BitmapImage(new Uri("/Elentok.Common.Wpf.Demos;component/Images/image1.png", UriKind.Relative))
      });

      vm.AddPage(new ImageWizardPageViewModel { Title = "This is page #2",
        Message = "This is the second page,\r\nhmm....",
        Image = new BitmapImage(new Uri("/Elentok.Common.Wpf.Demos;component/Images/image2.png", UriKind.Relative))
      });

      vm.AddPage(new ImageWizardPageViewModel
      {
        Title = "This is page #3",
        Message = "This is the third page,\r\nhmm....",
        Image = new BitmapImage(new Uri("/Elentok.Common.Wpf.Demos;component/Images/image3.png", UriKind.Relative))
      });

      vm.Show(false);
    }
  }
}
